package br.com.vacine;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Adapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import br.com.vacine.Entity.RowCalsse;
import br.com.vacine.Entity.VacinaEntity;
import br.com.vacine.Entity.VacinacaoEntity;
import br.com.vacine.dao.DB;
import br.com.vacine.util.CustomArrayAdapter;

public class HistoricoActivity extends ActionBarActivity{

	private List<VacinaEntity> lala = new ArrayList<VacinaEntity>();
	private List<VacinacaoEntity> listaVac = new ArrayList<VacinacaoEntity>();
	private int idVac;
	private int position;
	private	List<RowCalsse> rows;
	private ListView listView;
	private Button btExcluir;
	private TelaUsuarioActivity tela = new TelaUsuarioActivity();
	private List<VacinacaoEntity> listaAux = new ArrayList<VacinacaoEntity>();
	private DB db;
	protected boolean aux = false;


	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.historico);

        	db = new DB(this);
        	setDb(db); 
        	btExcluir = (Button)findViewById(R.id.btExcluirVac);
        
        	carregar();
        
        	excluirVacina();
    }
        
	@Override
	public void onResume(){
		super.onRestart();
		carregar();
	}
	
	public void excluirVacina(){
		
		btExcluir.setOnClickListener(new View.OnClickListener() {
			

			@SuppressWarnings("static-access")
			@Override
			public void onClick(View v) {
				try {
					if (listView != null) {
						Adapter adapter = (Adapter) listView.getAdapter();
 						for (int i = 0; i < adapter.getCount(); i++) {
							
 							RowCalsse rocl = (RowCalsse) adapter.getItem(i);

							if(rocl.isChecked()){
								aux = true;
								listaAux = getDb().listaFiltroExcluir(rocl.getId(),tela.getId(), rocl.getDataVac());
								getDb().delVacinaHistorico(listaAux.get(0).getIdVacinacao());
								listaAux.clear();
							}
						}
 						onResume();
						
					}
					if (aux == true) {
						
						Toast.makeText(getBaseContext(), R.string.msgExcluirVacinasHistorico, Toast.LENGTH_LONG).show();
					}else{
						Toast.makeText(getBaseContext(), R.string.msgNenhumSelect, Toast.LENGTH_LONG).show();
					}
					aux = false;
				} catch (Exception e) {
					
				}
				
				
			}
		});
		
		
	}

	@SuppressWarnings("static-access")
	public void carregar(){
		
       	limpar();

        listaVac = getDb().listHistorico(tela.getId());
        
        rows = new ArrayList<RowCalsse>();
        
        for (int i = 0; i < listaVac.size(); i++) {
        	
        	idVac = listaVac.get(i).getIdVacina();
        	lala = getDb().listVacinaHist(idVac);
        	
        	if(lala.size() != 0){
        		
        		RowCalsse row = new RowCalsse();

        		row.setId(idVac);
        		row.setTextNome(lala.get(0).getNomeVacina());
        		row.setDataVac(listaVac.get(i).getData());
        		
        		rows.add(row);
        	}
        	
        	
        	
        }
        listView = (ListView)findViewById(R.id.listViewCartoes);
        listView.setAdapter(new CustomArrayAdapter(this, rows));
    }
	
	private void limpar() {
		
		listaVac.clear();
        lala.clear();
        rows = null;
	}

	public int getPosition() {
		return position;
	}
	
	public void setPosition(int position) {
		this.position = position;
	}

	public ListView getListView() {
		return listView;
	}
	
	public void setListView(ListView listView) {
		this.listView = listView;
	}
	public DB getDb() {
		return db;
	}
	
	public void setDb(DB db) {
		this.db = db;
	}
}
